package com.xwolf.os.configuration;

import com.xwolf.os.Domain.MyBean;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * @author ming
 * @Description:
 * @create 2018-08-25 1:36 PM
 **/
@Component
@Log4j
public class DynamicConfiguration implements BeanFactoryPostProcessor {
    private DefaultListableBeanFactory beanFactory;



    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("ContextRefreshed...................");

        this.beanFactory = (DefaultListableBeanFactory)configurableListableBeanFactory;

        BeanDefinitionBuilder userBeanDefinitionBuilder = BeanDefinitionBuilder
                .genericBeanDefinition(MyBean.class);

        userBeanDefinitionBuilder.addPropertyValue("name", "chinfeng");
        userBeanDefinitionBuilder.addPropertyValue("password", "123456");
        beanFactory.registerBeanDefinition("user",
                userBeanDefinitionBuilder.getRawBeanDefinition());
    }
}
